package com.hljcj.boot.action;

import com.hljcj.boot.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping(value = "/")
@Slf4j
public class IndexAction {
    @Resource
    private LoginService loginService;


//    @RequestMapping(value = "/index", method = RequestMethod.GET)
//    public ModelAndView index(HttpServletRequest req) {
//        ModelAndView view = new ModelAndView("/index.html");
//        return view;
//    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView indexlogin(String mess) {
        log.info("info 登录");
        log.debug("debug 登录");
        ModelAndView mod = new ModelAndView("/login.html");
        if (null == mess) {
            mess = "";
        }
        mod.addObject("mess", mess);
        return mod;
    }


    @RequestMapping(value = "/login", method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView login(String userName, String passWord, HttpSession session) {

        ModelAndView mod = new ModelAndView();

        String mess = loginService.login(userName, passWord);

        if (mess.equals("succ")) {
            session.setAttribute("name", userName);
            mod.setViewName("/index.html");
            mod.addObject("mess", mess);
            return mod;
        } else {
            mod.addObject("mess", mess);
            mod.setViewName("/login.html");
            return mod;
        }
    }
}
